<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\adminchy\controller;


use app\adminchy\model\RoleModel;
use app\adminchy\controller\Base;
use app\adminchy\model\NoticeModel;

class Notice extends Base
{
    //用户列表
    public function index()
    {
        if(request()->isAjax()){
            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $user = new NoticeModel();
            $selectResult = $user->getUsersByWhere($offset, $limit);

            // 拼装参数
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }
            $return['total'] = $user->getAllUsers('');  //总数据
            $return['rows'] = $selectResult;

            return json($return);
        }
        return $this->fetch('Notice/index');
    }

    // 添加用户
    public function noticeAdd()
    {
        if(request()->isPost()){

            $param = input('post.');
            $user = new NoticeModel();
            $flag = $user->insertUser($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        return $this->fetch('Notice/notice_add');
    }

    // 编辑用户
    public function userEdit()
    {
        $user = new NoticeModel();

        if(request()->isPost()){

            $param = input('post.');
            $flag = $user->editUser($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        $id = input('param.id');
        $notice = $user->where('id', $id)->find();
        $this->assign([
            'notice' => $notice,
        ]);
        return $this->fetch('Notice/notice_edit');
    }

    // 删除用户
    public function userDel()
    {
        $id = input('param.id');

        $role = new NoticeModel();
        $flag = $role->delUser($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'Notice/useredit',
                'href' => url('Notice/userEdit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'Notice/userdel',
                'href' => "javascript:userDel(" .$id .")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }

}
